package main

import (
	"database/sql"
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"log"
)

func main() {
	db, _ := sql.Open(
		"mysql",
		"root:root@tcp(172.20.0.10:3306)/gogogo?charset=utf8&parseTime=true&loc=Local",
	)
	defer db.Close()
	updateStudentName(db, 1, "zhangsan1")
}

func updateStudentName(db *sql.DB, id int, name string) {
	stmt, _ := db.Prepare("update tbl_student set name = ? where id = ?")
	result, err := stmt.Exec(name, id)
	if err != nil {
		log.Println("update student error.", err.Error())
		return
	}
	rowsAffected, err := result.RowsAffected()
	if err != nil {
		log.Println("get row affected error", err)
		return
	}
	fmt.Println("update row num =", rowsAffected)
}

